#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, SQ = 450;
long long d[N], ans[SQ], mn[SQ], mx[SQ];
int n, m, h[N];
inline long long get(int st, int en) {
int l = en + 1, r = n + st;
long long res = 0, x = 1e18;
for (; l < r && l % SQ; x = min(x, d[l] - 2 * h[l]), l++)
res = max(res, d[l] + 2 * h[l] - x);
for (int i = l / SQ; i < r / SQ; x = min(x, mn[i++]), l += SQ)
res = max(res, max(ans[i], mx[i] - x));
for (; l < r; x = min(x, d[l] - 2 * h[l]), l++)
res = max(res, d[l] + 2 * h[l] - x);
return res;
}
inline void read_input() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> d[i];
d[n + i] = d[i];
}
for (int i = 0; i < n; i++) {
cin >> h[i];
h[n + i] = h[i];
}
}
inline void solve() {
memset(mn, 63, sizeof mn);
partial_sum(d, d + n + n, d);
for (int i = 0; i < SQ; i++)
for (int j = i * SQ; j < min(i * SQ + SQ, n + n); j++) {
mx[i] = max(mx[i], d[j] + 2 * h[j]);
ans[i] = max(ans[i], d[j] + 2 * h[j] - mn[i]);
mn[i] = min(mn[i], d[j] - 2 * h[j]);
}
}
inline void write_output() {
while (m--) {
int a, b;
cin >> a >> b;
if (--a > --b)
b += n;
cout << get(a, b) << endl;
}
}
int main() {
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
read_input(), solve(), write_output();
return 0;
}
12A - Super Agent | 1709A - Three Doors |
1680C - Binary String | 1684B - Z mod X = C |
1003A - Polycarp's Pockets | 1691B - Shoe Shuffling |
1706A - Another String Minimization Problem | 1695B - Circle Game |
1702B - Polycarp Writes a String from Memory | 1701A - Grass Field |
489C - Given Length and Sum of Digits | 886B - Vlad and Cafes |
915A - Garden | 356A - Knight Tournament |
1330A - Dreamoon and Ranking Collection | 1692B - All Distinct |
1156C - Match Points | 1675A - Food for Animals |
1328C - Ternary XOR | 1689A - Lex String |
1708B - Difference of GCDs | 863A - Quasi-palindrome |
1478A - Nezzar and Colorful Balls | 1581B - Diameter of Graph |
404A - Valera and X | 908A - New Year and Counting Cards |
146A - Lucky Ticket | 1594C - Make Them Equal |
1676A - Lucky | 1700B - Palindromic Numbers |